// ================================================================================
// 		File: PartyRepresentativeMeeting.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（）
// 		Date: 2016-03-23 01:45
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// 
// ================================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ZHDJ.Core;

namespace ZHDJ.Data.Models.Center
{
    /// <summary>
    /// 党代表工作室活动公告
    /// </summary>   
    public class PartyRepresentativeMeeting : EntityBase

    {
        #region 属性


        /// <summary>
        /// 工作室ID
        /// </summary>
        public string StudioID { get; set; }

        /// <summary>
        /// 主题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? BeginTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 状态（1草稿，4发布）
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 活动内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public int OrderNo { get; set; }

        /// <summary>
        /// 活动记录
        /// </summary>
        public string Record { get; set; }

        /// <summary>
        /// 创建用户
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改用户
        /// </summary>
        public string ModifyBy { get; set; }

        #endregion

        #region 查询管理

        public static DataSet GetGridData(int pageIndex, int pageSize, string status, string searchKey = "", string studioID = "",string representativeId="")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  data.ID,data.StudioID,data.Title,data.BeginTime,data.EndTime,
                        data.Status,data.Address,data.Content,data.Record,data.CreateBy,b.StudioName,
                        data.CreateTime,data.ModifyTime,data.ModifyBy,
                        case data.Status when 4 then '已发布' else '草稿' end as  StatusName
                                                from tblPartyRepresentativeMeeting data
                        left join tblPartyRepresentativeStudio b on(data.StudioID=b.ID)
                        where 1=1 ");
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and data.Title like ?key ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "key", Value = string.Format("%{0}%", searchKey) });
            }

            if (!string.IsNullOrEmpty(status))
            {
                sb.Append(" and data.status=?status");
                parameters.Add(new MySqlParameter() { DbType = DbType.Int32, ParameterName = "status", Value = status });
            }
            if (!string.IsNullOrEmpty(studioID))
            {
                sb.Append(" and data.studioID=?studioID ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "studioID", Value = studioID });
            }
            if (!string.IsNullOrEmpty(representativeId))
            {
                sb.Append(" and ?representativeId in (select RepresentativeID from tblPartyRepresentativeMeetingJoin b where b.MeetingID=data.ID) ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "representativeId", Value = representativeId });
            }
            sb.Append(" order by data.BeginTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }


        #endregion
    }
}
